package com.firefightingiot.firefightingiot.web.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONObject;
import com.firefightingiot.firefightingiot.common.base.BaseService;
import com.firefightingiot.firefightingiot.common.utils.appkit.ConfigKit;
import com.firefightingiot.firefightingiot.model.DeviceAlarmLog;
import com.firefightingiot.firefightingiot.model.DeviceBusiness;
import com.firefightingiot.firefightingiot.support.model.RespEntity;
import com.firefightingiot.firefightingiot.support.mybatisplus.QueryWrapper;
import com.firefightingiot.firefightingiot.support.utils.DayUtil;
import com.firefightingiot.firefightingiot.support.utils.MyLogger;
import com.firefightingiot.firefightingiot.support.utils.requestutil.HttpRequestUtil;
import com.firefightingiot.firefightingiot.web.mapper.DeviceAlarmLogMapper;
import com.firefightingiot.firefightingiot.web.mapper.DeviceBusinessMapper;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class ReportService extends BaseService {

    @Resource
    public MyLogger logger;


    @Resource
    private DeviceBusinessMapper deviceBusinessMapper;
    @Resource
    private DeviceAlarmLogMapper deviceAlarmLogMapper;

    /**
     * @param jsonParam
     * @return
     * @Title: getReport <br>
     * @Description: (下载报告) <br>
     * @author 石延杰<br>
     */

    public RespEntity getReport(JSONObject jsonParam) throws Exception {
        //token验证
        //处理支持form-data和json两种方式
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        //调用用户表
        com.alibaba.fastjson.JSONObject usre = userInfo(jsonParam, "systemsetup", "userInfo", "systemsetup");
        String homeId = usre.getString("homeId");
        String userType = usre.getString("type");
        String businessId = null;
        if (userType.equals("1")) {
            businessId = jsonParam.getStr("businessId");
        } else {
            businessId = homeId;
        }
        //单位详情
        JSONObject jsonParamN = new JSONObject();
        jsonParamN.put("id", businessId);
        com.alibaba.fastjson.JSONObject businessDetail = makeData(jsonParamN, "businessJava", "config", "getBusinessDetailNoToken", "business");
        String businessName = businessDetail.getString("name");
        //获取查询的日期
        String date = jsonParam.get("date").toString();//2020-07
        //获取查询日期对应的月初的日期
        String beginDate = date.replace("-", "") + "01";
        //判断服务器上有没有这个文件
        String fileName = businessName + "(" + businessId + ")" + beginDate + ".doc";
        String fileSavePath = ConfigKit.getStr("excelSavePath", "excel");
        String path = fileSavePath + fileName;
        File file = new File(path);
        if (!judeFileExists(file)) {//没有
            //生成报告，并返回路径
            fileName = getFileName(beginDate, businessId, businessName, businessDetail.getString("addr"), date);
            //返回二进制流
        }
        String uploadPath = ConfigKit.getStr("path", "excel");
        return RespEntity.success(uploadPath+fileName);
    }

    /**
     * 获取导出的数据
     *
     * @param beginDate
     * @param businessId
     * @param businessName
     * @param addr
     * @param date
     * @return
     * @throws Exception
     */
    public Map<String, Object> getMap(String beginDate, String businessId, String businessName, String addr, String date) throws Exception {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date queryDate = formatter.parse(date + "-01" + " 00:00:00");
        String monthBegin = DayUtil.getMonthStart(queryDate);
        String monthEnd = DayUtil.getMonthEnd(queryDate);
        //组装最终数据
        Map<String, Object> map = new HashMap<>();
        //单位名称
        map.put("businessName", businessName);
        //单位详细地址
        map.put("businessAddr", addr);
        map.put("nowDate", DateUtil.now().split(" ")[0].replace("-", ""));
        //设备详情
        List<DeviceBusiness> deviceBusiness = deviceBusinessMapper.selectList(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId));
        //安装终端设备列表
        //ArrayList<String> deviceList = new HashSet<String>();
        HashSet<String> hashSet = new HashSet<>();
        for (DeviceBusiness device : deviceBusiness) {
            if (device.getDeviceClassify().equals("1")) {
                //deviceList.add("智慧用电监测系统");
                hashSet.add("智慧用电监测系统");
                map.put("fireFlag", "1");
            }
            if (device.getDeviceClassify().equals("2")) {
                //deviceList.add("消防用水监测系统");
                hashSet.add("消防用水监测系统");
                map.put("waterFlag", "1");
            }
            if (device.getDeviceClassify().equals("3")) {
                //deviceList.add("智慧用电监测系统");
                hashSet.add("智慧型独立烟感");
                map.put("smokeFlag", "1");
            }
            if (device.getDeviceClassify().equals("4")) {
                //deviceList.add("可燃气体检测系统");
                hashSet.add("可燃气体检测系统");
                map.put("gasFlag", "1");
            }
            if (device.getDeviceClassify().equals("5")) {
                hashSet.add("火灾自动联网检测系统");
                map.put("fireFlag", "1");
            }
            if (device.getDeviceClassify().equals("6")) {
                hashSet.add("隐患视频识别系统");
                map.put("visFlag", "1");
            }
            if (device.getDeviceClassify().equals("7")) {
                hashSet.add("智能灭火器检测系统");
                map.put("extinguisherFlag", "1");
            }
            if (device.getDeviceClassify().equals("10")) {
                hashSet.add("水泵检测系统");
                map.put("waterAspiratorFlag", "1");
            }
            if (device.getDeviceClassify().equals("11")) {
                hashSet.add("智慧加油站");
                map.put("petrolStationFlag", "1");
            }
            if (device.getDeviceClassify().equals("12")) {
                hashSet.add("智慧楼梯");
                map.put("stairsFlag", "1");
            }
            if (device.getDeviceClassify().equals("13")) {
                hashSet.add("智慧水域");
                map.put("waterAreaFlag", "1");
            }
            if (device.getDeviceClassify().equals("15")) {
                hashSet.add("可视视频");
                map.put("viewFlag", "1");
            }
            if (device.getDeviceClassify().equals("19")) {
                hashSet.add("开关量检测系统");
                map.put("switchFlag", "1");
            }
            if (device.getDeviceClassify().equals("22")) {
                hashSet.add("充电桩安全");
                map.put("chargingPileFlag", "1");
            }
        }
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("queryStarttime", monthBegin);
        jsonParam.put("queryEndtime", monthEnd);
        jsonParam.put("businessId", businessId);
        //--------------------------安全隐患巡查系统---------------------------------
        Integer checkAddr = restEvaluation(jsonParam, "check", "getCheckaddrCount", "check");
        if (checkAddr > 0) {
            hashSet.add("安全隐患巡查系统");
            map.put("checkFlag", "1");
        }
        //安装设备列表
        map.put("deviceList", hashSet);
        //--------------------------防火巡查---------------------------------
        //rest调用获取巡检巡查报告数据
        Map<String, Object> check = restCheck(jsonParam, "check", "getCheckaddrForReportDoList", "check");
        //巡查标签总数
        map.put("checkBQnum", check.get("allSize"));
        //本月巡查总数
        map.put("monthCheckNum", check.get("yesSize"));
        //未巡查总数
        map.put("NoCheck", check.get("noSize"));
        //巡查使用率
        map.put("checkRate", check.get("rate"));
        //本月隐患
        Integer monthHiddendanger = restEvaluation(jsonParam, "check", "getMonthHiddendanger", "check");
        map.put("hiddendangerNum", monthHiddendanger);
        //本月已整改隐患
        Integer rectifyHiddendanger = restEvaluation(jsonParam, "check", "getRectifyHiddendanger", "check");
        map.put("rectifyHiddendangerNum", rectifyHiddendanger);
        //--------------------------智慧用电监测---------------------------------
//        if (map.containsKey("fireFlag")) {
        // 电设备总套数
        int electricityDeviceNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId).eq("device_classify", "1"));
        map.put("electricityDeviceNum", electricityDeviceNum);
        //电设备在线总数
        int electricityDeviceOnlineNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId).eq("currentstate", 1).eq("device_classify", "1"));
        map.put("electricityDeviceOnlineNum", electricityDeviceOnlineNum);
        //电设备报警总数
        JSONObject electricityDeviceAlarm = new JSONObject();
        electricityDeviceAlarm.put("businessId", businessId);
        electricityDeviceAlarm.put("deviceClassify", "1");
        electricityDeviceAlarm.put("queryStarttime", jsonParam.getStr("queryStarttime"));
        electricityDeviceAlarm.put("queryEndtime", jsonParam.getStr("queryEndtime"));
        int electricityDeviceAlarmNum = deviceAlarmLogMapper.getAlarmDeviceCount(electricityDeviceAlarm);
        map.put("electricityDeviceAlarmNum", electricityDeviceAlarmNum);
        //电设备断线总数
        int electricityDeviceDisconnectedNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId).eq("currentstate", 3).eq("device_classify", "1"));
        map.put("electricityDeviceDisconnectedNum", electricityDeviceDisconnectedNum);
        //电设备在线率
        map.put("electricityDeviceRate", electricityDeviceNum == 0 ? 0 + "%" : NumberUtil.roundStr(((double) electricityDeviceOnlineNum) * 100 / electricityDeviceNum, 2) + "%");
        //报警原因
        JSONObject electricityAlarmTypeName = new JSONObject();
        electricityAlarmTypeName.put("businessId", businessId);
        electricityAlarmTypeName.put("deviceClassify", "1");
        electricityAlarmTypeName.put("queryStarttime", jsonParam.getStr("queryStarttime"));
        electricityAlarmTypeName.put("queryEndtime", jsonParam.getStr("queryEndtime"));
        List<DeviceAlarmLog> electricitylogList = deviceAlarmLogMapper.getAlarmTypeName(electricityAlarmTypeName);
        String electricityAlarmName = "";
        for (DeviceAlarmLog str : electricitylogList) {
            electricityAlarmName = str.getAlarmTypeName() + "、" + electricityAlarmName;
        }
        if (!electricityAlarmName.isEmpty()){
            electricityAlarmName = electricityAlarmName.substring(0, electricityAlarmName.length() - 1);
        }
        map.put("electricityAlarmName", electricityAlarmName);
//        }

        //--------------------------建筑消防用水监测---------------------------------
        //水设备总套数
        int waterDeviceNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId).eq("device_classify", "2"));
        map.put("waterDeviceNum", waterDeviceNum);
        //水设备在线总数
        int waterDeviceOnlineNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId).eq("currentstate", 1).eq("device_classify", "2"));
        map.put("waterDeviceOnlineNum", waterDeviceOnlineNum);
        //水设备报警总数
        JSONObject waterDeviceAlarm = new JSONObject();
        waterDeviceAlarm.put("businessId", businessId);
        waterDeviceAlarm.put("deviceClassify", "2");
        waterDeviceAlarm.put("queryStarttime", jsonParam.getStr("queryStarttime"));
        waterDeviceAlarm.put("queryEndtime", jsonParam.getStr("queryEndtime"));
        int waterDeviceAlarmNum = deviceAlarmLogMapper.getAlarmDeviceCount(waterDeviceAlarm);
        map.put("waterDeviceAlarmNum", waterDeviceAlarmNum);
        //水设备断线总数
        int waterDeviceDisconnectedNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId).eq("currentstate", 3).eq("device_classify", "2"));
        map.put("waterDeviceDisconnectedNum", waterDeviceDisconnectedNum);
        //水设备在线率
        map.put("waterDeviceRate", waterDeviceNum == 0 ? 0 + "%" : NumberUtil.roundStr(((double) waterDeviceOnlineNum) * 100 / waterDeviceNum, 2) + "%");
        //报警原因
        JSONObject waterAlarmTypeName = new JSONObject();
        waterAlarmTypeName.put("businessId", businessId);
        waterAlarmTypeName.put("deviceClassify", "2");
        waterAlarmTypeName.put("queryStarttime", jsonParam.getStr("queryStarttime"));
        waterAlarmTypeName.put("queryEndtime", jsonParam.getStr("queryEndtime"));
        List<DeviceAlarmLog> waterlogList = deviceAlarmLogMapper.getAlarmTypeName(waterAlarmTypeName);
        String waterAlarmName = "";
        for (DeviceAlarmLog str : waterlogList) {
            waterAlarmName = str.getAlarmTypeName() + "、" + waterAlarmName;
        }
        if (!waterAlarmName.isEmpty()){
            waterAlarmName = waterAlarmName.substring(0, waterAlarmName.length() - 1);
        }
        map.put("waterAlarmName", waterAlarmName);
        //--------------------------智慧独立烟感---------------------------------
        // 烟雾设备总套数
        int smokeDeviceNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId).eq("device_classify", "3"));
        map.put("smokeDeviceNum", smokeDeviceNum);
        // 烟雾设备在线总数
        int smokeDeviceOnlineNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId).eq("currentstate", 1).eq("device_classify", "3"));
        map.put("smokeDeviceOnlineNum", smokeDeviceOnlineNum);
        // 烟雾设备报警总数
        JSONObject smokeDeviceAlarm = new JSONObject();
        smokeDeviceAlarm.put("businessId", businessId);
        smokeDeviceAlarm.put("deviceClassify", "3");
        smokeDeviceAlarm.put("queryStarttime", jsonParam.getStr("queryStarttime"));
        smokeDeviceAlarm.put("queryEndtime", jsonParam.getStr("queryEndtime"));
        int smokeDeviceAlarmNum = deviceAlarmLogMapper.getAlarmDeviceCount(smokeDeviceAlarm);
        map.put("smokeDeviceAlarmNum", smokeDeviceAlarmNum);
        // 烟雾设备断线总数  currentstate：1 正常  2 报警 3 离线
        int smokeDeviceDisconnectedNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId).eq("currentstate", 3).eq("device_classify", "3"));
        map.put("smokeDeviceDisconnectedNum", smokeDeviceDisconnectedNum);
        // 烟雾设备在线率
        map.put("smokeDeviceRate", smokeDeviceNum == 0 ? 0 + "%" : NumberUtil.roundStr(((double) smokeDeviceOnlineNum) * 100 / smokeDeviceNum, 2) + "%");
        // 报警原因
        JSONObject smokeAlarmTypeName = new JSONObject();
        smokeAlarmTypeName.put("businessId", businessId);
        smokeAlarmTypeName.put("deviceClassify", "3");
        smokeAlarmTypeName.put("queryStarttime", jsonParam.getStr("queryStarttime"));
        smokeAlarmTypeName.put("queryEndtime", jsonParam.getStr("queryEndtime"));
        List<DeviceAlarmLog> smokelogList = deviceAlarmLogMapper.getAlarmTypeName(smokeAlarmTypeName);
        String smokeAlarmName = "";
        for (DeviceAlarmLog str : smokelogList) {
            smokeAlarmName = str.getAlarmTypeName() + "、" + smokeAlarmName;
        }
        if (!smokeAlarmName.isEmpty()){
            smokeAlarmName = smokeAlarmName.substring(0, smokeAlarmName.length() - 1);
        }
        map.put("smokeAlarmName", smokeAlarmName);
        //--------------------------开关量检测-------------------------------
        // 开关量设备总套数
        int switchDeviceNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId).eq("device_classify", "19"));
        map.put("switchDeviceNum", switchDeviceNum);
        //--------------------------火灾自动联网---------------------------------
        // 火灾联网设备总套数
        int fireDeviceNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId).eq("device_classify", "5"));
        map.put("fireDeviceNum", fireDeviceNum);
        // 火灾联网设备在线总数
        int fireDeviceOnlineNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId).eq("currentstate", 1).eq("device_classify", "5"));
        map.put("fireDeviceOnlineNum", fireDeviceOnlineNum);
        // 火灾联网设备报警总数
        JSONObject fireDeviceAlarm = new JSONObject();
        fireDeviceAlarm.put("businessId", businessId);
        fireDeviceAlarm.put("deviceClassify", "5");
        fireDeviceAlarm.put("queryStarttime", jsonParam.getStr("queryStarttime"));
        fireDeviceAlarm.put("queryEndtime", jsonParam.getStr("queryEndtime"));
        int fireDeviceAlarmNum = deviceAlarmLogMapper.getAlarmDeviceCount(fireDeviceAlarm);
        map.put("fireDeviceAlarmNum", fireDeviceAlarmNum);
        // 火灾联网设备断线总数  currentstate：1 正常  2 报警 3 离线
        int fireDeviceDisconnectedNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId).eq("currentstate", 3).eq("device_classify", "5"));
        map.put("fireDeviceDisconnectedNum", fireDeviceDisconnectedNum);
        // 火灾联网设备在线率
        map.put("fireDeviceRate", fireDeviceNum == 0 ? 0 + "%" : NumberUtil.roundStr(((double) fireDeviceOnlineNum) * 100 / fireDeviceNum, 2) + "%");
        // 报警原因
        JSONObject fireAlarmTypeName = new JSONObject();
        fireAlarmTypeName.put("businessId", businessId);
        fireAlarmTypeName.put("deviceClassify", "5");
        fireAlarmTypeName.put("queryStarttime", jsonParam.getStr("queryStarttime"));
        fireAlarmTypeName.put("queryEndtime", jsonParam.getStr("queryEndtime"));
        List<DeviceAlarmLog> firelogList = deviceAlarmLogMapper.getAlarmTypeName(fireAlarmTypeName);
        String fireAlarmName = "";
        for (DeviceAlarmLog str : firelogList) {
            fireAlarmName = str.getAlarmTypeName() + "、" + fireAlarmName;
        }
        if (!fireAlarmName.isEmpty()){
            fireAlarmName = fireAlarmName.substring(0, fireAlarmName.length() - 1);
        }
        map.put("fireAlarmName", fireAlarmName);
        //--------------------------可燃气体检测---------------------------------
        // 可燃气体检测总套数
        int gasDeviceNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId).eq("device_classify", "4"));
        map.put("gasDeviceNum", gasDeviceNum);
        // 可燃气体检测设备在线总数
        int gasDeviceOnlineNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId).eq("currentstate", 1).eq("device_classify", "4"));
        map.put("gasDeviceOnlineNum", gasDeviceOnlineNum);
        // 可燃气体检测设备报警总数
        JSONObject gasDeviceAlarm = new JSONObject();
        gasDeviceAlarm.put("businessId", businessId);
        gasDeviceAlarm.put("deviceClassify", "4");
        gasDeviceAlarm.put("queryStarttime", jsonParam.getStr("queryStarttime"));
        gasDeviceAlarm.put("queryEndtime", jsonParam.getStr("queryEndtime"));
        int gasDeviceAlarmNum = deviceAlarmLogMapper.getAlarmDeviceCount(gasDeviceAlarm);
        map.put("gasDeviceAlarmNum", gasDeviceAlarmNum);
        // 可燃气体检测设备断线总数  currentstate：1 正常  2 报警 3 离线
        int gasDeviceDisconnectedNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId).eq("currentstate", 3).eq("device_classify", "4"));
        map.put("gasDeviceDisconnectedNum", gasDeviceDisconnectedNum);
        // 可燃气体检测设备在线率
        map.put("gasDeviceRate", gasDeviceNum == 0 ? 0 + "%" : NumberUtil.roundStr(((double) gasDeviceOnlineNum) * 100 / gasDeviceNum, 2) + "%");
        // 报警原因
        JSONObject gasAlarmTypeName = new JSONObject();
        gasAlarmTypeName.put("businessId", businessId);
        gasAlarmTypeName.put("deviceClassify", "4");
        gasAlarmTypeName.put("queryStarttime", jsonParam.getStr("queryStarttime"));
        gasAlarmTypeName.put("queryEndtime", jsonParam.getStr("queryEndtime"));
        List<DeviceAlarmLog> gaslogList = deviceAlarmLogMapper.getAlarmTypeName(gasAlarmTypeName);
        String gasAlarmName = "";
        for (DeviceAlarmLog str : gaslogList) {
            gasAlarmName = str.getAlarmTypeName() + "、" + gasAlarmName;
        }
        if (!gasAlarmName.isEmpty()){
            gasAlarmName = gasAlarmName.substring(0, gasAlarmName.length() - 1);
        }
        map.put("gasAlarmName", gasAlarmName);
        //--------------------------慧眼识别---------------------------------
        // 可视化设备总套数
        int visualizationDeviceNum = deviceBusinessMapper.selectCount(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId).eq("device_classify", "6"));
        map.put("visualizationDeviceNum", visualizationDeviceNum);
        //报警原因
        JSONObject visualizationAlarmTypeName = new JSONObject();
        visualizationAlarmTypeName.put("businessId", businessId);
        visualizationAlarmTypeName.put("deviceClassify", "6");
        visualizationAlarmTypeName.put("queryStarttime", jsonParam.getStr("queryStarttime"));
        visualizationAlarmTypeName.put("queryEndtime", jsonParam.getStr("queryEndtime"));
        List<DeviceAlarmLog> visualizationlogList = deviceAlarmLogMapper.getAlarmTypeName(visualizationAlarmTypeName);
        String visualizationAlarmName = "";
        for (DeviceAlarmLog str : visualizationlogList) {
            visualizationAlarmName = str.getAlarmTypeName() + "、" + visualizationAlarmName;
        }
        if (!visualizationAlarmName.isEmpty()){
            visualizationAlarmName = visualizationAlarmName.substring(0, visualizationAlarmName.length() - 1);
        }
        map.put("visualizationAlarmName", visualizationAlarmName);
        //报警次数
        int visualizationAlarmNum = deviceAlarmLogMapper.selectCount(new QueryWrapper<DeviceAlarmLog>().eq("business_id", businessId).eq("device_classify", "6"));
        map.put("visualizationAlarmNum", visualizationAlarmNum);
        //智慧用电本月报警日志
//        electricityAlarmTypeName.put("queryStarttime", jsonParam.getStr("queryStarttime"));
//        electricityAlarmTypeName.put("queryEndtime", jsonParam.getStr("queryEndtime"));
        List<DeviceAlarmLog> electricityMonthAlarmlog = deviceAlarmLogMapper.getMonthAlarmlog(electricityAlarmTypeName);
//        for (DeviceAlarmLog log : electricityMonthAlarmlog) {
////            System.out.println(log);
//            if (StringUtil.strIsNullOrEmpty((String) log.get("name"))) {
//                    log.set("name", "null");
//            }
//        }
        map.put("electricityMonthAlarmlog", electricityMonthAlarmlog);
        //消防用水本月报警日志
        List<DeviceAlarmLog> waterMonthAlarmlog = deviceAlarmLogMapper.getMonthAlarmlog(waterAlarmTypeName);
        map.put("waterMonthAlarmlog", waterMonthAlarmlog);
        // 独立烟感本月报警日志
        List<DeviceAlarmLog> smokeMonthAlarmlog = deviceAlarmLogMapper.getMonthAlarmlog(smokeAlarmTypeName);
        map.put("smokeMonthAlarmlog", smokeMonthAlarmlog);
        // 火灾自动检测本月报警日志
        List<DeviceAlarmLog> fireMonthAlarmlog = deviceAlarmLogMapper.getMonthAlarmlog(fireAlarmTypeName);
        map.put("fireMonthAlarmlog", fireMonthAlarmlog);
        // 可燃气体检测本月报警日志
        List<DeviceAlarmLog> gasMonthAlarmlog = deviceAlarmLogMapper.getMonthAlarmlog(gasAlarmTypeName);
        map.put("gasMonthAlarmlog", gasMonthAlarmlog);
        // 慧眼识别检测本月报警日志
        List<DeviceAlarmLog> visMonthAlarmlog = deviceAlarmLogMapper.getMonthAlarmlog(visualizationAlarmTypeName);
        map.put("visMonthAlarmlog", visMonthAlarmlog);

        return map;
    }

    public String getFileName(String beginDate, String businessId, String businessName, String addr, String date) throws Exception {
        Map<String, Object> dataMap = getMap(beginDate, businessId, businessName, addr, date);
        //Configuration 用于读取ftl文件
        Configuration configuration = new Configuration(new Version("2.3.30"));
        configuration.setDefaultEncoding("utf-8");

        /**
         * 以下是两种指定ftl文件所在目录路径的方式，注意这两种方式都是
         * 指定ftl文件所在目录的路径，而不是ftl文件的路径
         * 指定路径的第一种方式（根据某个类的相对路径指定）
         */
        //configuration.setClassForTemplateLoading(this.getClass(), "");
        //指定路径的第二种方式，我的路径是C：/a.ftl
        configuration.setDirectoryForTemplateLoading(new File(ConfigKit.getStr("templates", "excel")));
        String fileSavePath = ConfigKit.getStr("excelSavePath", "excel");
        //单位名称(单位id)+"2020072"
        String fileName = businessName + "(" + businessId + ")" + beginDate + ".doc";
        //输出文档路径及名称
        File outFile = new File(fileSavePath + fileName);
        //以utf-8的编码读取ftl文件
        Template template = configuration.getTemplate("report.ftl", "utf-8");
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
        template.process(dataMap, out);
        out.close();
        return fileName;
    }

    // 判断文件是否存在
    public boolean judeFileExists(File file) {
        boolean flag = false;
        if (file.exists()) {
            flag = true;
        } else {
            flag = false;
        }
        return flag;
    }

}
